package com.example.wc.logindemo.getdownload;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.wc.logindemo.Blog;
import com.example.wc.logindemo.EditBlogActivity;
import com.example.wc.logindemo.R;
import com.example.wc.logindemo.VolleyRequestDemo;

import java.util.List;

/**
 * Created by hui on 2017/2/19.
 */

public class PhotoAdapter extends RecyclerView.Adapter <PhotoAdapter.BlogHolder>{
    private LayoutInflater layoutInflater;
    private Context mContext;

    private RequestQueue mQueue;
//    private VolleyRequestDemo mVolleyRequestDemo;
    private ImageLab.Images mImages;
    private List<Image> mImageList;
    private ImageLoader imageLoader;
    private ImageLoader.ImageListener listener;


    public PhotoAdapter(Context context, List<Image> imageList){
        mContext = context;
        mImageList = imageList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public BlogHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.photo_list_item, parent, false);
        return new BlogHolder(view);
    }

    @Override
    public void onBindViewHolder(BlogHolder holder, int position) {
        holder.bindCrime();
        imageLoader.get(mImageList.get(position).getUrl(), listener, 200 ,200);
    }

    @Override
    public int getItemCount() {
        //todo缓存图片数量
        return mImageList.size();
    }


    class BlogHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private ImageView mImageView;

        public BlogHolder(View itemView) {
            super(itemView);
            //ViewHolder监听器接口1/2：单个item设置监听器
            itemView.setOnClickListener(this);

            mImageView = (ImageView) itemView.findViewById(R.id.photo_image_view);
        }

        public void bindCrime(){

            mQueue = Volley.newRequestQueue(mContext);
            imageLoader = new ImageLoader(mQueue, new BitmapCache());

            listener = ImageLoader.getImageListener(mImageView,
                    R.drawable.ic_blogs_add, R.mipmap.ic_launcher);


//            mImageView.setImageResource(R.mipmap.ic_launcher);
//            mImageView.setImageBitmap();
            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    VolleyRequestDemo volleyRequestDemo = new VolleyRequestDemo();
//                    volleyRequestDemo.volleyRequestDemo_POST_Remove(mContext, mBlog.getId(), new VolleyRequestDemo.BlogPostListener() {
//                        @Override
//                        public void onBlogPosted() {
//                            Log.d("DELETE", "OK");
//                            Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
////                            Intent i = new Intent(mContext, BlogsListActivity.class);
////                            mContext.startActivity(i);
//                            //notifyItemRemoved(position);
//                            mBlogs.remove(blog);
//                            notifyDataSetChanged();
//                            // TODO: 2017/3/14 刷新BlogsListFragment
//                        }
//                    });
                    Log.d("aaaaaaaaaaaa", "OK");
                }
            });

        }

        @Override
        public void onClick(View v) {
            //ViewHolder监听器接口2/2：单个item设置监听器，点击跳转到WriteActivity
//            Intent intent = new Intent(mContext, WriteBlogActivity.class);
            //传入2/3:调用WriteActivity.newIntent()方法传递blogId到WriteActivity的Intent中
//            Intent intent = EditBlogActivity.newIntent(mContext, mBlog.getId());
//            mContext.startActivity(intent);
        }
    }

}


